﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ElectronicJournal.DB.Item;

namespace ElectronicJournal.Account.AuthorAccount
{
    public partial class PublicationsWaitingForPayment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource4.SelectParameters[0].DefaultValue = Membership.GetUser().ProviderUserKey.ToString();
        }

        protected void AddNewAuthor(object sender, EventArgs e)
        {
            SqlDataSource3.InsertParameters[0].DefaultValue = PublicationIdLabel.Text;
            SqlDataSource3.Insert();
        }

        protected void AddNewPublication(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            string folder = Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\";
            System.IO.Directory.CreateDirectory(folder);

            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);
            string strArticle = item.FileArticle;
            string strReview1 = item.FileReview1;
            string strReview2 = item.FileReview2;
            string strLetter = item.FileLetter;
            string strExpert = item.FileExpert;

            if (FileArticleUpload.Visible)
            {
                strArticle = UploadFileOnServer(FileArticleUpload, folder);
            }
            if (FileReview1Upload.Visible)
            {
                strReview1 = UploadFileOnServer(FileReview1Upload, folder);
            }
            if (FileReview2Upload.Visible)
            {
                strReview2 = UploadFileOnServer(FileReview2Upload, folder);
            }
            if (FileLetterUpload.Visible)
            {
                strLetter = UploadFileOnServer(FileLetterUpload, folder);
            }
            if (FileExpertUpload.Visible)
            {
                strExpert = UploadFileOnServer(FileExpertUpload, folder);
            }

            DB.Item.ItemPublication newItem = new DB.Item.ItemPublication()
            {
                //AuthorId = Membership.GetUser().ProviderUserKey.ToString(),
                TitleRussian = TitleRussianTextBox.Text,
                TitleEnglish = TitleEnglishTextBox.Text,
                ScientificDirection = ScientificDropDownList.SelectedValue,
                Type = TypeDropDownList.SelectedValue,
                UDCIndex = UDCIndexTextBox.Text,
                Speciality = SpecialityTextBox.Text,
                SummaryRussian = SummaryRussianTextBox.Text,
                SummaryEnglish = SummaryEnglishTextBox.Text,
                KeywordsRussian = KeywordsRussianTextBox.Text,
                KeywordsEnglish = KeywordsEnglishTextBox.Text,
                FileArticle = strArticle,
                FileReview1 = strReview1,
                FileReview2 = strReview2,
                FileLetter = strLetter,
                FileExpert = strExpert,
                Date = now,
                State = 0,
                Id = PublicationIdLabel.Text
            };
            if (new DB.DbPublications().UpdatePublication(newItem))
            {
                PublicationGridView.Visible = true;
                PublicationDataPanel.Visible = false;
                PaymentPanel.Visible = false;
                PublicationIdLabel.Text = "";
            }
            else
            {
                ErrorLabel.Text = "Введены неверные данные";
            }
        }

        private string UploadFileOnServer(FileUpload uploader, string folder)
        {

            string FileName = folder + System.IO.Path.GetFileName(uploader.FileName);
            if ((uploader.PostedFile != null) && (uploader.PostedFile.ContentLength > 0))
            {
                uploader.PostedFile.SaveAs(FileName);
            }
            return FileName;
        }

        protected void ShowPublicationData(object sender, EventArgs e)
        {
            PublicationIdLabel.Text = PublicationGridView.SelectedDataKey.Value.ToString();
            SqlDataSource3.SelectParameters[0].DefaultValue = PublicationIdLabel.Text;
            
            PublicationGridView.Visible = false;
            PublicationDataPanel.Visible = true;

            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);

            TitleRussianTextBox.Text = item.TitleRussian;
            TitleEnglishTextBox.Text = item.TitleEnglish;
            ScientificDropDownList.SelectedValue = item.ScientificDirection;
            TypeDropDownList.SelectedValue = item.Type;
            UDCIndexTextBox.Text = item.UDCIndex;
            SpecialityTextBox.Text = item.Speciality;
            SummaryRussianTextBox.Text = item.SummaryRussian;
            SummaryEnglishTextBox.Text = item.SummaryEnglish;
            KeywordsRussianTextBox.Text = item.KeywordsRussian;
            KeywordsEnglishTextBox.Text = item.KeywordsEnglish;
            FileArticleLinkButton.Text = item.FileArticle;
            FileReview1LinkButton.Text = item.FileReview1;
            FileReview2LinkButton.Text = item.FileReview2;
            FileLetterLinkButton.Text = item.FileLetter;
            FileExpertLinkButton.Text = item.FileExpert;
            ErrorLabel.Text = "";
        }

        protected void ShowPublicationGridView(object sender, EventArgs e)
        {
            PublicationGridView.Visible = true;
            PublicationDataPanel.Visible = false;
            PaymentPanel.Visible = false;
            PublicationIdLabel.Text = "";
        }

        protected void DeleteFileArticle(object sender, EventArgs e)
        {
            FileArticleLinkButton.Visible = false;
            FileArticleButton.Visible = false;
            FileArticleUpload.Visible = true;
        }

        protected void DeleteFileReview1(object sender, EventArgs e)
        {
            FileReview1LinkButton.Visible = false;
            FileReview1Button.Visible = false;
            FileReview1Upload.Visible = true;
        }

        protected void DeleteFileReview2(object sender, EventArgs e)
        {
            FileReview2LinkButton.Visible = false;
            FileReview2Button.Visible = false;
            FileReview2Upload.Visible = true;
        }

        protected void DeleteFileLetter(object sender, EventArgs e)
        {
            FileLetterLinkButton.Visible = false;
            FileLetterButton.Visible = false;
            FileLetterUpload.Visible = true;
        }

        protected void DeleteFileExpert(object sender, EventArgs e)
        {
            FileExpertLinkButton.Visible = false;
            FileExpertButton.Visible = false;
            FileExpertUpload.Visible = true;
        }

        protected void ShowPaymentPanel(object sender, EventArgs e)
        {
            PublicationIdLabel.Text = PublicationGridView.SelectedDataKey.Value.ToString();

            PublicationDataPanel.Visible = false;
            PaymentPanel.Visible = true;
        }

        protected void SetStatePaid(object sender, EventArgs e)
        {
            new DB.DbPublications().UpdatePublicationState(PublicationIdLabel.Text, 1);
            PublicationGridView.Visible = true;
            PublicationDataPanel.Visible = false;
            PaymentPanel.Visible = false;
            PublicationIdLabel.Text = "";
        }

        protected void DownloadFileArticle(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileArticle;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\" + item.FileArticle);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void DownloadFileReview1(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileReview1;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\" + item.FileReview1);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void DownloadFileReview2(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileReview2;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\" + item.FileReview2);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void DownloadFileLetter(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileLetter;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\" + item.FileLetter);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void DownloadFileExpert(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(PublicationIdLabel.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileExpert;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\" + item.FileExpert);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }
    }
}